home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / unix / bsd / telldir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-01  |  2.5 KB  |  94 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <dirent.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <stdlib.h>
  26.  
  27. /* Internal data structure for telldir and seekdir.  */
  28. struct record
  29.   {
  30.     struct record *next; /* Link in chain.  */
  31.     off_t cookie;        /* Value returned by `telldir'.  */
  32.     off_t pos;
  33.     size_t offset;
  34.   };
  35. #define NBUCKETS 32
  36. static struct record *records[32];
  37. static off_t lastpos;
  38.  
  39.  
  40. /* Return the current position of DIRP.  */
  41. off_t
  42. DEFUN(telldir, (dirp), DIR *dirp)
  43. {
  44.   struct record *new;
  45.  
  46.   new = malloc (sizeof *new);
  47.   if (new == NULL)
  48.     return (off_t) -1;
  49.  
  50.   new->pos = dirp->__pos;
  51.   new->offset = dirp->__offset;
  52.   new->cookie = ++lastpos;
  53.   new->next = records[new->cookie % NBUCKETS];
  54.   records[new->cookie % NBUCKETS] = new;
  55.  
  56.   return new->cookie;
  57. }
  58.  
  59.  
  60.  
  61. /* Seek to position POS in DIRP.  */
  62. void
  63. DEFUN(seekdir, (dirp, pos), DIR *dirp AND __off_t pos)
  64. {
  65.   struct record *r, **prevr;
  66.  
  67.   for (prevr = &records[pos % NBUCKETS], r = *prevr;
  68.        r != NULL;
  69.        prevr = &r->next, r = r->next)
  70.     if (r->cookie == pos)
  71.       {
  72.     if (dirp->__pos != r->pos || dirp->__offset != r->offset)
  73.       {
  74.         dirp->__size = 0;    /* Must read a fresh buffer.  */
  75.         /* Move to the saved position.  */
  76.         __lseek (dirp->__fd, r->pos, SEEK_SET);
  77.         dirp->__pos = r->pos;
  78.         dirp->__offset = 0;
  79.         /* Read entries until we reach the saved offset.  */
  80.         while (dirp->__offset < r->offset)
  81.           if (readdir (dirp) == NULL)
  82.         break;
  83.       }
  84.  
  85.     /* To prevent leaking memory, cookies returned from telldir
  86.        can only be used once.  So free this one's record now.  */
  87.     *prevr = r->next;
  88.     free (r);
  89.     return;
  90.       }
  91.  
  92.   /* We lost, but have no way to indicate it.  Oh well.  */
  93. }
  94.